import java.util.Scanner;

public class IntersectingPoint {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        System.out.println("=== 测试 9.12 交点计算 ===");
        System.out.println("请输入第一条线段的两个端点:");
        System.out.print("x1 y1: ");
        double x1 = input.nextDouble();
        double y1 = input.nextDouble();
        System.out.print("x2 y2: ");
        double x2 = input.nextDouble();
        double y2 = input.nextDouble();

        System.out.println("请输入第二条线段的两个端点:");
        System.out.print("x3 y3: ");
        double x3 = input.nextDouble();
        double y3 = input.nextDouble();
        System.out.print("x4 y4: ");
        double x4 = input.nextDouble();
        double y4 = input.nextDouble();

        // 计算直线方程的系数
        double a = y1 - y2;
        double b = x2 - x1;
        double e = a * x1 + b * y1;
        
        double c = y3 - y4;
        double d = x4 - x3;
        double f = c * x3 + d * y3;

        // 创建 LinearEquation 对象
        LinearEquation equation = new LinearEquation(a, b, c, d, e, f);

        if (equation.isSolvable()) {
            double x = equation.getX();
            double y = equation.getY();
            System.out.printf("交点坐标: (%.5f, %.5f)\n", x, y);
        } else {
            System.out.println("两条直线平行，没有交点。");
        }

        input.close();
    }
}

class LinearEquation {
    private double a;
    private double b;
    private double c;
    private double d;
    private double e;
    private double f;

    public LinearEquation(double a, double b, double c, double d, double e, double f) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
        this.e = e;
        this.f = f;
    }

    public double getA() { return a; }
    public double getB() { return b; }
    public double getC() { return c; }
    public double getD() { return d; }
    public double getE() { return e; }
    public double getF() { return f; }

    public boolean isSolvable() {
        return (a * d - b * c) != 0;
    }

    public double getX() {
        if (!isSolvable()) {
            throw new RuntimeException("方程无解");
        }
        return (e * d - b * f) / (a * d - b * c);
    }

    public double getY() {
        if (!isSolvable()) {
            throw new RuntimeException("方程无解");
        }
        return (a * f - e * c) / (a * d - b * c);
    }
}